package com.budwk.app.util.poiUtils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrFormatter;
import com.budwk.app.sqfw.dto.DaYingLuStreetFireHiddenCheckWordDTO;
import com.budwk.app.sqfw.enums.DownLoadFileNameEnum;
import com.budwk.app.sqfw.vo.GenerateWordAndPdfUrlVO;
import com.deepoove.poi.data.PictureRenderData;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.budwk.app.util.poiUtils.CommonFormOfficeUtils.*;

/**
 * @author zhangp
 * @date 2023-11-30
 *
 * <p>
 * 大营路街道社会治安文档生成工具类
 * </p>
 */
public class DaYingLuStreetPublicSecurityOfficeUtils {


    /**
     * 创建现场照片pdf
     *
     * @param formSummary 现场照片填表数据
     * @return 结果
     */
    private static ByteArrayOutputStream createSitePicture(DaYingLuStreetFireHiddenCheckWordDTO formSummary) {
        List<SitePictureOfficeUtils.SitePictureContent> sitePictureContentList = Lists.newArrayList();
        if (StringUtils.isNotBlank(formSummary.getA9())) {
            List<String> pictureList = Lists.newArrayList(Lists.newArrayList(formSummary.getA9().split(",")));
            String textStr = "附件：{} {} 走访照片";
            textStr = StrFormatter.format(textStr, formSummary.getA1(), DateUtil.format(formSummary.getCreateDate(), "yyyy-MM-dd"));
            sitePictureContentList.add(new SitePictureOfficeUtils.SitePictureContent(textStr, pictureList));
        }
        return SitePictureOfficeUtils.createSitePicture(sitePictureContentList);
    }


    /**
     * 生成pdf
     *
     * @param formSummary
     * @return
     */
    public static GenerateWordAndPdfUrlVO createPublicSecurityPDF(DaYingLuStreetFireHiddenCheckWordDTO formSummary) {
        List<ByteArrayOutputStream> osList = Lists.newArrayList();
        // 大营路街道社会治安巡查表
        ByteArrayOutputStream smallBusinessPremisesSafetyOS = createPublicSecurityRecord(formSummary);
        osList.add(smallBusinessPremisesSafetyOS);

        // 现场照片
        /*ByteArrayOutputStream sitePictureOS = createSitePicture(formSummary);
        osList.add(sitePictureOS);*/

        //String fileName = StrFormatter.format("{}社会治安表{}", formSummary.getA1(), DateUtil.format(new Date(), "yyyyMMdd"));
        //zhangp 2023-12-13 文件名称格式统一
        String downloadFileName = DownLoadFileNameEnum.DYL_PUBLIC_SECURITY_STANDING_PDF.getName();
        String fileName = StrFormatter.format("{}{}{}", formSummary.getA1(), downloadFileName, DateUtil.format(new Date(), "yyyyMMdd"));

        return wordToPdfAndUpload(osList, fileName, formSummary.getUnitName() + "街道社会治安巡查表");
    }

    /**
     * 构建数据
     *
     * @param formSummary
     * @return
     */
    private static ByteArrayOutputStream createPublicSecurityRecord(DaYingLuStreetFireHiddenCheckWordDTO formSummary) {

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("county", formSummary.getCounty());
        paramMap.put("unitName", formSummary.getUnitName());
        paramMap.put("fillDateStr", DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd"));
        paramMap.put("a1", formSummary.getA1());//场所名称
        paramMap.put("a2", formSummary.getA2());//场所负责人
        paramMap.put("a3", formSummary.getA3());//场所地址
        paramMap.put("a5", formSummary.getA5());//场所类型
        paramMap.put("a8", formSummary.getA8());//负责人电话
        //String a9 = formSummary.getA9();
        //paramMap.put("a9", a9);//走访图片
       /* if (StringUtils.isNotBlank(a9)) {
            List<byte[]> a9Bytes = CommonFormOfficeUtils.addPictureToByteList(a9, 1);
            paramMap.put("a9", a9Bytes);//走访图片
        }*/
        String pictureUrlStr = formSummary.getA9();
        List<Map<String, PictureRenderData>> pictures = Lists.newArrayList();
        if (StringUtils.isNotBlank(pictureUrlStr)) {
            List<String> pictureUrlList = Lists.newArrayList(pictureUrlStr.split(","));
            pictures = CommonFormOfficeUtils.addWordPicture(pictureUrlList, "a9", 5F);
        }
        paramMap.put("a9s", pictures);//走访图片

        paramMap.put("a16", formSummary.getA16());//具体分类
        String a20 = formSummary.getA20();//隐患描述
        if ("无".equals(a20)) {
            paramMap.put("a20", a20);
            paramMap.put("a20_1", "");
        } else {
            paramMap.put("a20", "有");
            paramMap.put("a20_1", a20);
        }
        String a68 = formSummary.getA68();//有无涉黑涉恶线索
        if (StringUtils.isNotBlank(a68) && !"无".equals(a68)) {
            paramMap.put("a68", "有");
            paramMap.put("a68_1", a68);
        } else {
            paramMap.put("a68", a68);
            paramMap.put("a68_1", "");
        }
        addWordUserSignature(paramMap, "a18NoSig", "a18Sigs", "a18Sig", formSummary.getA18NoSignatureUsers(), formSummary.getA18UserSignatureUrls(), 1.0F);
        PictureRenderData a19Pic = addWordPicture(formSummary.getA19(), 1.0F);
        paramMap.put("a19", a19Pic);

        return generateWordDocument(paramMap, "daYingLu/publicSecurityCheck.docx", "大营路街道社会治安重点风险场所巡查表", System.currentTimeMillis());

    }
}
